/**
 * AluiCommons -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of AluiCommons.
 * 
 * AluiCommons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AluiCommons is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AluiCommons.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using com.plumtree.server;
using com.plumtree.openlog;

namespace AluiServerLib.Models
{
    class PTBaseObjectModel
    {
        private static OpenLogger log = OpenLogService.GetLogger(
            "AluiServerLib",
            "AluiServerLib.Models.PTBaseObjectModel");

        protected IPTSession m_ptSession = null;
        
        public PTBaseObjectModel(IPTSession ptSession)
        {
            m_ptSession = ptSession;
        }

        public void Release()
        {
            m_ptSession = null;
        }

        public IPTSession PTSession
        {
            get
            {
                return m_ptSession;
            }
        }

        public int getUserID()
        {
            return m_ptSession.GetUser().GetObjectID();
        }

        public int getObjectID(String uuid)
        {
            if (null != uuid)
            {
                Object[] oClassIDObjectID = getClassObjectID(uuid);
                if (null != oClassIDObjectID)
                {
                    try
                    {
                        return int.Parse(oClassIDObjectID[1].ToString());
                    }
                    catch (Exception exc)
                    {
                        return 0;
                    }
                }
            }
            return 0;
        }

        public int getClassID(String uuid)
        {
            if (null != uuid)
            {
                Object[] oClassIDObjectID = getClassObjectID(uuid);
                if (null != oClassIDObjectID)
                {
                    try
                    {
                        return int.Parse(oClassIDObjectID[0].ToString());
                    }
                    catch (Exception exc)
                    {
                        return 0;
                    }
                }
            }
            return 0;
        }

        public Object[] getClassObjectID(String uuid)
        {
            Object[] oClassIDObjectID = null;
            if (null != uuid)
            {
                try
                {
                    IPTMigrationManager oPTMigrationMgr = (IPTMigrationManager)m_ptSession
                                    .OpenGlobalObject(
                                                    PT_GLOBALOBJECTS.PT_GLOBAL_MIGRATION_MANAGER,
                                                    false);
                    oClassIDObjectID = oPTMigrationMgr.UUIDToObjectID(uuid);
                }
                catch (Exception exc)
                {
                    oClassIDObjectID = null;
                }
            }
            return oClassIDObjectID;
        }
    }
}
